/*global Adebug*/
// log-model.js - A mongoose model
//
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.
const Powers = require('../apis/powers')

module.exports = function (app) {
  const mongooseClient = app.get('mongooseClient')
  const { Schema } = mongooseClient
  const log = new Schema({
    optime: { type: Date, required: true, index: true },
    type: { type: String, required: true, index: true }, // 日志类型
    optype: { type: String, required: true, index: true },
    code: { type: Number }, // 推送返回状态码
    hs: { type: Number }, // 耗时
    opuser: { type: Schema.Types.ObjectId, index: true, sparse: true },
    opname: { type: String },
    appid: { type: Schema.Types.ObjectId, index: true, sparse: true },
    appname: { type: String },
    mark: { type: String },
    ip: { type: String },
    agent: { type: String },
    g: { type: Schema.Types.Mixed },
    p: { type: Schema.Types.Mixed },
    // r: { type: Schema.Types.Mixed }, // 推送返回内容
  })

  // 添加日志
  log.statics.addByHook = (type, start, app, optype, d, res) => {
    var save = {
      appid: app._id,
      appname: app.name,
      optime: new Date(),
      type: type,
      hs: (new Date()).getTime() - start,
      optype: optype,
      p: d.data,
      code: res.status,
      r: res.data
    }
    save.mark = d.result.oaid
    sDb.create(save)
  }

  // uri转日志代码
  const uri2LogCode = Object.assign({
    '/account/login': 'accountLogin',
    '/account/logout': 'accountLogout',
    '/account/password': 'accountPasswd',
    '/staff/findOne': 'staffFindOne',
    '/staff/remove': 'staffRemove',
  }, Powers.apiToCode, {
    '/app/enable': (req) => {
      if (req.body.enable === true) return 'appEnableTrue'
      return 'appEnableFalse'
    }
  }, {
    '/api/staff/create': 'staffCreate',
    '/api/staff/update': 'staffUpdate',
    '/api/staff/update_passwd': 'staffPasswd',
    '/api/staff/find': 'staffFind',
    '/api/staff/findOne': 'staffFindOne',
    '/api/staff/login': 'staffLogin'
  })

  // 无需记录日志的路由
  const noLogUris = [
    '/account/info',
    '/log/find',
    '/app/find',
    '/role/find',
    '/admin/find', '/admin/count',
    '/staff/find', '/staff/findOne'
  ]

  // 添加接口日志
  log.statics.addByApi = (start, req, code, err) => {
    // 过滤路由
    if (noLogUris.indexOf(req.baseUrl) !== -1) return
    // 转换为日志代码
    var logCode = uri2LogCode[req.baseUrl] || req.baseUrl
    if (typeof(logCode) === 'function') logCode = logCode(req)

    let save = {
      type: 'admin',
      ip: req.Aip,
      optime: start,
      hs: (new Date()).getTime() - start,
      optype: logCode,
      g: req.query,
      p: req.body
    }
    // 账号模块
    if (req.baseUrl === '/account/login') {
      // save.type = 'login'
      save.code = code
      save.opname = req.body.username || ''
      save.mark = '登录成功'
      if (code !== 200) {
        save.mark = err.message
      }
    }
    if (err && err.message) {
      save.mark = err.message
    }
    // 服务接口
    if (req.baseUrl.substr(0, 5) === '/api/') {
      save.type = 'api'
    }
    // 三方登录模块
    if (req.baseUrl === '/api/account/login') {
      save.type = 'login'
      save.code = code
      save.opname = req.body.username || ''
      save.mark = '登录成功'
      if (code !== 200) {
        save.mark = err.message
      }
    }

    save.agent = req.headers['user-agent'] || ''
    if (req.Apps) {
      save.appid = req.Apps._id
      save.appname = req.Apps.name
      if (code) save.code = code
    }
    if (req.user && req.user._id) {
      save.opuser = req.user._id
      save.opname = req.user.username
    }
    if (req.baseUrl.substr(0, 6) === '/test/') {
      save.type = 'test'
      save.appname = 'test'
      save.code = code
    }
    sDb.create(save)
  }
  const sDb = mongooseClient.model('log', log)
  return sDb
}
